// 获取时间戳
const getTimestamp = () => {
	return Date.parse(new Date())
}
// 2019-06-18
const getNowFormatDate = () => {
	let date = new Date();
	let seperator1 = "-";
	let year = date.getFullYear();
	let month = date.getMonth() + 1;
	let strDate = date.getDate();
	if (month >= 1 && month <= 9) {
		month = "0" + month;
	}
	if (strDate >= 0 && strDate <= 9) {
		strDate = "0" + strDate;
	}
	let currentdate = year + seperator1 + month + seperator1 + strDate;
	return currentdate;
}
const beforetoday = (date) => {
	let date1 = new Date(date) //要对比的时间
	let date2 = new Date(); //获取当前时间对象
	let num = 24 * 60 * 60 * 1000; //一天的毫秒数
	let cha = date2.getTime() - date1.getTime(); //两个时间的毫秒差
	if (cha > 0) {
		return "before"
	} else if (cha < 0) {
		// console.log("是未来时间")
		return "future"
	} else {
		return "now"
	}
}
// Format("yyyy-MM-dd");
// Format("yyyy-MM-dd HH:mm:ss");  
const Format = function(timestamp, fmt,extype) {
	if(extype == "unix"){
		timestamp = timestamp*1000
	}
	var date = new Date(timestamp);
	console.log(date)
	var YY = date.getFullYear() + '-';
	var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
	var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
	var hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
	var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
	var ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
	if (fmt.toLowerCase() == "yy-mm-dd") {
		return YY + MM + DD
	} else {
		return YY + MM + DD + " " + hh + mm + ss
	}
}
const sleep = (time) => {
	return new Promise(resolve => {
		setTimeout(() => {
			resolve()
		}, time * 1000)
	})
}
const convertTimeWithPoint = function(timeStamp) {
	var date = new Date(timeStamp);
	let Y = date.getFullYear() + "."
	let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + "."
	let D = date.getDate() + ""
	return Y + M + D
}

function timeago(dateTimeStamp){   //dateTimeStamp是一个时间毫秒，注意时间戳是秒的形式，在这个毫秒的基础上除以1000，就是十位数的时间戳。13位数的都是时间毫秒。
    var minute = 1000 * 60;      //把分，时，天，周，半个月，一个月用毫秒表示
    var hour = minute * 60;
    var day = hour * 24;
    var week = day * 7;
    var halfamonth = day * 15;
    var month = day * 30;
    var now = new Date().getTime();   //获取当前时间毫秒
    console.log(now)
    var diffValue = now - dateTimeStamp;//时间差

    if(diffValue < 0){
        return;
    }
    var minC = diffValue/minute;  //计算时间差的分，时，天，周，月
    var hourC = diffValue/hour;
    var dayC = diffValue/day;
    var weekC = diffValue/week;
    var monthC = diffValue/month;
    if(monthC >= 1 && monthC <= 3){
        result = " " + parseInt(monthC) + "月前"
    }else if(weekC >= 1 && weekC <= 3){
        result = " " + parseInt(weekC) + "周前"
    }else if(dayC >= 1 && dayC <= 6){
        result = " " + parseInt(dayC) + "天前"
    }else if(hourC >= 1 && hourC <= 23){
        result = " " + parseInt(hourC) + "小时前"
    }else if(minC >= 1 && minC <= 59){
        result =" " + parseInt(minC) + "分钟前"
    }else if(diffValue >= 0 && diffValue <= minute){
        result = "刚刚"
    }else {
        var datetime = new Date();
        datetime.setTime(dateTimeStamp);
        var Nyear = datetime.getFullYear();
        var Nmonth = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
        var Ndate = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
        var Nhour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
        var Nminute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
        var Nsecond = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
        result = Nyear + "-" + Nmonth + "-" + Ndate
    }
    return result;
}

export default {
	getTimestamp,
	getNowFormatDate,
	beforetoday,
	Format,
	sleep,
	convertTimeWithPoint,
	timeago
}
